(function($, room) {
	//var room = room || {};
	var state = app.getState();
	var uid = state.user_id;
	//localStorage.removeItem('qunlist' + uid);
	var qdata = JSON.parse(localStorage.getItem('qunlist' + uid)) || [];
	//console.log(JSON.stringify(qdata));
	room.add = function(info) {
		qdata.push(info);
		localStorage.setItem('qunlist' + uid, JSON.stringify(qdata));
	}
	room.del = function(room_id) {
		var news = [],
			len = qdata.length,
			i = 0;
		for(; i < len; i++) {
			if(qdata[i]['room_id'] != room_id) {
				news.push(qdata[i]);
			}
		}
		localStorage.setItem('qunlist' + uid, JSON.stringify(news));
	}
	room.edit = function(info) {
		var news = [],
			len = qdata.length,
			i = 0;
		for(; i < len; i++) {
			if(qdata[i]['room_id'] != info.room_id) {
				news.push(qdata[i]);
			} else {
				news.push(info);
			}
		}
		localStorage.setItem('qunlist' + uid, JSON.stringify(news));
	}
	/*
	room.get = function(room_id) {
		var data = room.getOne(room_id);
		setTimeout(function(){
			room.set(room_id, 'edit');
		},1000);
	}*/
	room.get	 = function(room_id) {
		var news, len = qdata.length,
			i = 0,
			has = false;
		for(; i < len; i++) {
			if(qdata[i]['room_id'] == room_id) {
				news = qdata[i];
				has = true;
				break;
			}
		}
		if(has === false) {
			return room.set(room_id, 'add');
		}
		setTimeout(function(){
			room.set(room_id, 'edit');
		},1000);
		return news;
	};
	room.set = function(room_id, type) {
		$.ajaxSettings.async = false;
		var geturl = apiurl + 'chat/getRoomDetail?room_id=' + room_id;
		var rt;
		$.getJSON(geturl, function(res) {
			if(res.status > 0) {
				if(type === 'edit') {
					room.edit(res);
				}else{
					room.add(res);
				}
				rt = res;
			}
		});
		return rt;
	};
	room.list = function(){
		var news = [];
		for(var i = 0; i < qdata.length; i++) {
			news.push(qdata[i]['info']);
		}
	}
}(mui, window.room = {}));